package com.lyf.emall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lyf.emall.bean.Page;
import com.lyf.emall.bean.ResponseResult;
import com.lyf.emall.bean.SysAuthority;
import com.lyf.emall.mapper.AuthorityMapper;
import com.lyf.emall.service.AuthorityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Description:
 * @Author Lfy
 * @Date 2022/4/8-15:21
 */
@Service
public class AuthorityServiceImpl implements AuthorityService {
    @Autowired
    AuthorityMapper authorityMapper;
    @Override
    public ResponseResult getAllAuthority(Page page) {
        if (page.getPageNo() < 0){
            new ResponseResult(400, "页码不能为负数");
        }
        PageHelper.startPage(page.getPageNo(), page.getPageNum());
        List<SysAuthority> sysAuthorities = authorityMapper.queryAllAuthority();

        PageInfo<SysAuthority> pageInfo = new PageInfo<>(sysAuthorities);
        Map<String,PageInfo> map = new HashMap<>();
        map.put("list", pageInfo);
        return new ResponseResult(200, "获取权限列表成功", map);
    }

    @Override
    public ResponseResult getAllAuthority() {
        List<SysAuthority> sysAuthorities = authorityMapper.queryAllAuthority();
        System.out.println(sysAuthorities.size());
        return new ResponseResult(200, "获取权限信息成功", sysAuthorities);
    }

    @Override
    public ResponseResult isAuthority(String authorityKey) {
        if (!StringUtils.hasText(authorityKey)){
            return new ResponseResult(400, "权限字段不能为空");
        }
        SysAuthority sysAuthority = authorityMapper.queryAuthorityByAuthorityKey(authorityKey);
        if (Objects.isNull(sysAuthority)){
            return new ResponseResult(200, "可以添加权限字段");
        }
        return new ResponseResult(400, "权限字段重名");
    }

    @Override
    public ResponseResult addAuthority(SysAuthority sysAuthority) {
        Integer insertAuthority = authorityMapper.insertAuthority(sysAuthority);
        if (insertAuthority <= 0){
            return new ResponseResult(400, "添加权限信息失败");
        }
        return new ResponseResult(200, "添加权限信息成功");
    }

    @Override
    public ResponseResult updateAuthority(SysAuthority sysAuthority) {
        if (!StringUtils.hasText(sysAuthority.getAuthorityName()) || !StringUtils.hasText(sysAuthority.getAuthorityKey())){
            return new ResponseResult(400, "字段不能为空");
        }
        Integer updateAuthority = authorityMapper.updateAuthority(sysAuthority);
        if (updateAuthority <= 0){
            return new ResponseResult(400, "修改权限信息失败");
        }
        return new ResponseResult(200, "修改权限信息成功");
    }

    @Override
    public ResponseResult deleteAuthority(Integer authorityId) {
        Integer deleteAuthority = authorityMapper.deleteAuthority(authorityId);
        if (deleteAuthority <= 0){
            return new ResponseResult(400, "删除权限信息失败");
        }
        return new ResponseResult(200, "删除权限信息成功");
    }
}
